/**
 * @author Jiayan
 */

/**
 * 
 * @param {Object} elem
 * @param {Object} attribute
 * @param {Object} value
 */

InforsenseTestUtils={
	parentWithAttributeValue:function(elem,attribute,value){
	if (!elem){ return false; }
		var parent = elem;
		while (parent){
			if (parent.getAttribute(attribute) == value){
				return parent; 
			}
			parent = parent.parentNode;
		}
		return false;
	}
	,
	parentWithAttribute:function(elem,attribute){
	if (!elem){ return false; }
		var parent = elem;
		while (parent){
			if (parent.getAttribute(attribute)){
				return parent; 
			}
			parent = parent.parentNode;
		}
		return false;
	}
,
	attributeValueFindParent:function(elem,attribute){
		var parent=InforsenseTestUtils.parentWithAttribute(elem,attribute);
		if (parent){
			return parent.getAttribute(attribute);
		}else{
			return false;
		}
	}
};
RecoderDragger={
	getX : function(a_object) {
		var vv = a_object.offsetLeft - a_object.scrollLeft;
		if (a_object.offsetParent) {
			vv = vv + this.getX(a_object.offsetParent)
		}		
		return vv;
	},

	getY : function(a_object) {
		var vv = a_object.offsetTop - a_object.scrollTop;
		if (a_object.offsetParent) {
			vv = vv + this.getY(a_object.offsetParent)
		}		
		return vv;
	}
	
};

Recorder.addEventHandler('prepareChartDragDrop', 'mousedown', function(a_event) {
		if (a_event.button==0){
			
			RecoderDragger.targetId = "";	
			RecoderDragger.isClick = true;
			var o = a_event.srcElement || a_event.target;
			
			var reg = /\w*_img/;
			var reg2 = /\w*_map/
			RecoderDragger.dragElement = null;
			if (o.id && reg.test(o.id) && o.tagName == "IMG") {
				RecoderDragger.dragElement = o;
			} else if (o.parentNode.id && reg2.test(o.parentNode.id) && o.parentNode.tagName == "MAP") {
				RecoderDragger.targetId = o.parentNode.id.substring(0, 36)
				RecoderDragger.dragElement = document.getElementById(RecoderDragger.targetId + "_img")
			}
				
			if (RecoderDragger.dragElement) {
				RecoderDragger.offsetDivX = RecoderDragger.getX(RecoderDragger.dragElement);
				RecoderDragger.offsetDivY = RecoderDragger.getY(RecoderDragger.dragElement);
				if (!a_event.pageX) {
					a_event.pageX = a_event.clientX + document.body.scrollLeft - document.body.clientLeft;
				}
				if (!a_event.pageY) {
					a_event.pageY = a_event.clientY + document.body.scrollTop - document.body.clientTop;
				}

				RecoderDragger.startX = a_event.clientX - RecoderDragger.offsetDivX;
				RecoderDragger.startY = a_event.clientY - RecoderDragger.offsetDivY;
				
				RecoderDragger.startPageX = a_event.pageX;
				RecoderDragger.startPageY = a_event.pageY;
				RecoderDragger.stopX = a_event.pageX;
				RecoderDragger.stopY = a_event.pageY;
				
				//this.setBegin(a_event.pageX, a_event.pageY);
				RecoderDragger.targetId = RecoderDragger.dragElement.id.substring(0, 36);
				RecoderDragger.dragElementId = RecoderDragger.dragElement.id.substring(0, 36);
			} else {
				RecoderDragger.dragElement = null;
				return;
			}

//			if (a_event.preventDefault) {
//				a_event.preventDefault();
//			}
			
			return;
			
		}
	});


Recorder.addEventHandler('chartSelection', 'mouseup', function(event) {
		
		if(event.button==0){
			//alert(RecoderDragger.dragElement);
			if (RecoderDragger.dragElement) {
				var imgElement=RecoderDragger.dragElement;
				RecoderDragger.dragElement = null;
//				RecoderDragger.dragDiv.style.display = "none";
			} else {
				return;
			}
	
			RecoderDragger.stopX = event.clientX - RecoderDragger.offsetDivX;
			RecoderDragger.stopY = event.clientY - RecoderDragger.offsetDivY;
			
			if (RecoderDragger.stopX == RecoderDragger.startX) {				
				return;
			}
			
			
			var s=RecoderDragger.startX+","+RecoderDragger.startY+","+RecoderDragger.stopX+","+RecoderDragger.stopY;
			
			//Find Locator
			
			var widgetId=InforsenseTestUtils.attributeValueFindParent(imgElement,"widgetid");
			//span[@widgetid='TextInputRenderer1223512212704165']//div[@class='inputContainer']//input
			//	var locator="//img[starts-with(@src,'/chart/do/drawChart')][position()=?]"
			var locator="//span[@widgetid='"+widgetId+"']//img[starts-with(@src,'/chart/do/drawChart')]";	
			
			this.record("chartSelection", locator, s);
			
		}
		
	});

LocatorBuilders.builderMap["id"]=function(e){
	if (e.id) {
		if (e.id.indexOf("ext-gen") == 0) {
			return null;
	//		return e.id;
		}
		else {
			return e.id;
		}
	}
	return null;

};
